export const getChartOptions = (titleName, data) => {
  // data 期望结构： {
  //   x: number[] (index数组),
  //   score: number[],
  //   threshold: number (常量),
  //   anomalyX?: number[], // prediction=1 的 index 集合
  //   anomalyY?: number[]  // 对应的 score 值
  // }
  const xAxisData = data?.x || Array.from({ length: 24 }, (_, i) => i)
  const scoreData = data?.score || []
  const threshold = data?.threshold ?? null
  const anomalyX = data?.anomalyX || []
  const anomalyY = data?.anomalyY || []
  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: { type: 'line' },
      formatter: (params) => {
        // params: array of series data on this axis position
        const idx = params?.[0]?.axisValue
        const scoreItem = params.find((p) => p.seriesName === '异常得分')
        const thresholdItem = params.find((p) => p.seriesName === '阈值')
        const anomalyItem = params.find((p) => p.seriesName === '异常点')
        const scoreVal = scoreItem ? scoreItem.data : '-'
        const thresholdVal = thresholdItem ? threshold : '-'
        const anomalyTag = anomalyItem ? '<span style="color:#F39C12">异常点</span>' : ''
        return `index: ${idx}<br/>异常得分: ${scoreVal}<br/>阈值: ${thresholdVal}<br/>${anomalyTag}`
      }
    },
    title: {
      text: `${titleName}调令异常得分`,
      bottom: 0, // 标题放底部
      left: 'center',
      textStyle: {
        fontSize: 18,
        fontWeight: 'bold'
        // color: '#fff'
      }
    },
    grid: {
      top: 100,
      right: 50,
      bottom: 60,
      left: 50
    },
    legend: {
      data: ['异常得分', '阈值', '异常点'],
      top: 10,
      textStyle: {
        // color: '#fff',
        fontSize: 16
      }
    },
    xAxis: {
      type: 'category',
      // name: 'index',
      data: xAxisData,
      axisLabel: {
        interval: 49
        // color: '#fff'
      },
      axisLine: {
        lineStyle: {
          // color: '#fff'
        }
      },
      axisTick: {
        interval: 49
      },
      nameTextStyle: {
        // color: '#fff'
      }
    },
    yAxis: {
      name: 'score',
      type: 'value',
      axisLabel: {
        // color: '#fff'
      },
      axisLine: {
        lineStyle: {
          // color: '#fff'
        }
      },
      nameTextStyle: {
        // color: '#fff'
      }
    },
    series: [
      {
        name: '异常得分',
        type: 'line',
        smooth: true,
        symbol: 'none',
        symbolSize: 6,
        itemStyle: {
          color: '#5B9BD5'
        },
        lineStyle: {
          width: 2
        },
        data: scoreData
      },
      // 阈值红色虚线（如果提供 threshold）
      ...(threshold !== null
        ? [
            {
              name: '阈值',
              type: 'line',
              data: xAxisData.map(() => threshold),
              symbol: 'none',
              itemStyle: {
                color: '#E74C3C'
              },
              lineStyle: {
                color: '#E74C3C',
                width: 2,
                type: 'dashed'
              }
            }
          ]
        : []),
      // 异常点（prediction=1）
      ...(anomalyX.length
        ? [
            {
              name: '异常点',
              type: 'scatter',
              data: anomalyX.map((x, i) => [x, anomalyY[i]]),
              symbol: 'circle',
              symbolSize: 6,
              itemStyle: {
                color: '#F39C12'
              },
              z: 3,
              emphasis: { focus: 'series' }
            }
          ]
        : [])
    ]
  }
}
